All files / local memory_persistence.ts

100% Statements 36/36
100% Branches 2/2
100% Functions 8/8
100% Lines 34/34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                                  2x 2x   2x 2x 2x             2x           2x               350x 350x 350x   350x   2x 350x 350x   350x     2x   327x 327x 327x     2x 347x 347x 332x 332x   347x     2x 339x     2x 332x     2x       6130x 6130x   2x     2x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { User } from '../auth/user';
import { assert } from '../util/assert';
import { debug } from '../util/log';
 
import { MemoryMutationQueue } from './memory_mutation_queue';
import { MemoryQueryCache } from './memory_query_cache';
import { MemoryRemoteDocumentCache } from './memory_remote_document_cache';
import { MutationQueue } from './mutation_queue';
import { Persistence, PersistenceTransaction } from './persistence';
import { PersistencePromise } from './persistence_promise';
import { QueryCache } from './query_cache';
import { RemoteDocumentCache } from './remote_document_cache';
 
const LOG_TAG = 'MemoryPersistence';
 
/**
 * A memory-backed instance of Persistence. Data is stored only in RAM and
 * not persisted across sessions.
 */
export class MemoryPersistence implements Persistence {
  /**
   * Note that these are retained here to make it easier to write tests
   * affecting both the in-memory and IndexedDB-backed persistence layers. Tests
   * can create a new LocalStore wrapping this Persistence instance and this
   * will make the in-memory persistence layer behave as if it were actually
   * persisting values.
   */
  private mutationQueues: { [user: string]: MutationQueue } = {};
  private remoteDocumentCache = new MemoryRemoteDocumentCache();
  private queryCache = new MemoryQueryCache();
 
  private started = false;
 
  start(): Promise<void> {
    assert(!this.started, 'MemoryPersistence double-started!');
    this.started = true;
    // No durable state to read on startup.
    return Promise.resolve();
  }
 
  shutdown(): Promise<void> {
    // No durable state to ensure is closed on shutdown.
    assert(this.started, 'MemoryPersistence shutdown without start!');
    this.started = false;
    return Promise.resolve();
  }
 
  getMutationQueue(user: User): MutationQueue {
    let queue = this.mutationQueues[user.toKey()];
    if (!queue) {
      queue = new MemoryMutationQueue();
      this.mutationQueues[user.toKey()] = queue;
    }
    return queue;
  }
 
  getQueryCache(): QueryCache {
    return this.queryCache;
  }
 
  getRemoteDocumentCache(): RemoteDocumentCache {
    return this.remoteDocumentCache;
  }
 
  runTransaction<T>(
    action: string,
    operation: (transaction: PersistenceTransaction) => PersistencePromise<T>
  ): Promise<T> {
    debug(LOG_TAG, 'Starting transaction:', action);
    return operation(new MemoryPersistenceTransaction()).toPromise();
  }
}
 
/** Dummy class since memory persistence doesn't actually use transactions. */
class MemoryPersistenceTransaction implements PersistenceTransaction {}